/**
 * @param {number[]} asteroids
 * @return {number[]}
 */
export var asteroidCollision = function (asteroids) {
  const stack = []
  for (const asteroid of asteroids) {
    if (asteroid < 0) {
      while (
        stack.length > 0 &&
        stack[stack.length - 1] > 0 &&
        stack[stack.length - 1] < -asteroid
      ) {
        stack.pop()
      }
    }
    if (stack.length === 0 || asteroid > 0 || stack[stack.length - 1] < 0) {
      stack.push(asteroid)
      continue
    }
    if (stack[stack.length - 1] === -asteroid) {
      stack.pop()
    }
  }
  return stack
}
